﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.IO;
using System.Web.Script.Serialization;

namespace JogoUno
{
    /// <summary>
    /// Interaction logic for CriarSala.xaml
    /// </summary>
    public partial class CriarSala : Window
    {


        public CriarSala()
        {

            InitializeComponent();
        }

        //botao criar sala
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                String nomeSala = textBox1.Text;
                String qt = (String) comboBox1.SelectedValue;

                if (qt != "")
                {

                    string resposta = MainWindow.enviarMensagem("http://" + MainWindow.servidor + ":9000/uno/sala/"+ MainWindow.jogador.idJogador +"/"+ nomeSala +"/"+ qt, "");
                    if (resposta != null) {
                        JavaScriptSerializer json = new JavaScriptSerializer();
                        var data = json.Deserialize<dynamic>(resposta);
                        string sucesso = data["sucesso"];
                        if (sucesso.Length > 0)
                        {
                            MainWindow.nomeSala = nomeSala;
                            MainWindow.lider = true;

                            Sala sala = (Sala)MainWindow.exibirJanela(MainWindow.JANELA_SALA);
                            this.Hide();
                            sala.Show();
                        }
                    }
                    
                    
/*

                    MainWindow.writer.WriteLine("criarsala" + MainWindow.separador + nomeSala + MainWindow.separador + qt); //envia msg ao servidor

                    String[] resposta = MainWindow.reader.ReadLine().Split('|');

                    if (resposta[0] == "OK")
                    {

                        MainWindow.nomeSala = nomeSala;
                        MainWindow.lider = true;

                        Sala sala = (Sala)MainWindow.exibirJanela(MainWindow.JANELA_SALA);
                        this.Hide();
                        sala.Show();
                       
                    }
                    else
                    {
                        //MessageBox.Show(this, resposta[1]);
                    }
*/                }
                else
                {
                    MessageBox.Show(this, "Selecione a quantidade.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro: " + ex.Message);
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            Menu menu = (Menu)MainWindow.exibirJanela(MainWindow.JANELA_MENU);
            this.Hide();
            menu.Show();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Menu menu = (Menu)MainWindow.exibirJanela(MainWindow.JANELA_MENU);
            menu.Show();
            MainWindow.fechar(MainWindow.JANELA_CRIAR_SALA);
        }
    }
}
